package ferp.android.tasks.center;

import android.content.Context;
import android.os.AsyncTask;
import ferp.android.managers.ProfileManager;
import ferp.center.client.Action;
import ferp.center.network.request.RequestLeaderboardGet;
import ferp.center.network.response.ResponseLeaderboardGet;
import ferp.core.Version;
import ferp.core.log.Log;
import ferp.core.player.Profile;

/**
 * User: olegs
 * Date: 29/05/12 09:49
 */
public class TaskTopPlayersGet extends AsyncTask<Void, Void, Void>
{
  public interface Listener
  {
    void onBeforeGet();
    void onAfterGet(ResponseLeaderboardGet response);
  }

  public TaskTopPlayersGet(Context context, Profile profile, Listener listener)
  {
    this.context = context;
    this.profile = profile;
    this.version = ProfileManager.regular(context).version();
    this.listener = listener;
  }

  @Override
  protected void onPreExecute()
  {
    listener.onBeforeGet();
  }

  @Override
  protected Void doInBackground(Void... voids)
  {
    try
    {
      RequestLeaderboardGet request = new RequestLeaderboardGet();

      request.version = version;
      request.profile = profile.centerId;
      request.configuration = profile.getConfiguration();
      request.start = 0;
      request.count = 10;

      response = Action.execute(Action.LEADERBOARD_GET, request, ResponseLeaderboardGet.class);
/*
      response = new ResponseLeaderboardGet();

      if (profile.centerId > 0)
      {
        Random random = new Random(System.currentTimeMillis());
        response.rank = random.nextInt(13);
      }

      for (int i = 0; i < 10; i++)
      {
        ResponseRating rr = new ResponseRating();
        rr.name = i == response.rank ? profile.nickname : "Oleg adcfafr234234r2raeffesdsd" + (i + 1);
        rr.profile = i == response.rank ? profile.centerId : i;
        rr.value = 5000000 - i;
        rr.games = 40000;
        rr.pool = 60000;
        response.list.add(rr);
      }
*/
    }
    catch (Exception e)
    {
      Log.error(Action.TAG, e);
    }

    return null;
  }

  @Override
  protected void onPostExecute(Void aVoid)
  {
    listener.onAfterGet(response);
  }

  private final Context context;
  private final Profile profile;
  private final Version version;
  private final Listener listener;
  private ResponseLeaderboardGet response;
}
